<?php
namespace Admin\Controller;
use Think\Controller;
class ActivityController extends PublicController {
	
	
	//活动列表
	public function listAction(){
		$Model=D("Activity"); // 实例化admin对象
		
		//搜索
		$keyword=I('get.keyword');
		$bend=I('get.bend');
		if(!empty($keyword)){
			$where['title']  = array('like', '%'.$keyword.'%');
			$map['_complex'] = $where;	
		}
		if($bend != -1){
			$time=date("Y-m-d H:i:s ",time());
			switch($bend)
			{
				case 1:
					$map['formtiem']  = array('gt',$time);
				break;
				case 2:
					$map['formtiem']  = array('lt',$time);
					$map['endtiem']  = array('gt',$time);
				break;
				case 3:
					$map['endtiem']  = array('lt',$time);
				break;
				default;
			}
		}
		
		//读取配置文件数据
		$Conf=D("Config"); // 实例化config对象
		$conf_one=$Conf->where(' id=1 ')->find();

		$count      = $Model->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$conf_one['Psize']);// 实例化分页类 传入总记录数和每页显示的记记录数
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');
		
		$Page->setConfig('header','<li><span class="rows">共 %TOTAL_ROW% 条记录</span></li>');
	
		
		$Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %PAGES%');
		
		$list = $Model->where($map)->order(' aid asc ')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出

		$this->display();
		}
		
		
		
	//活动增加
	public function saveAction(){
		$Model=D("Activity"); // 实例化admin对象
		
		//编辑
		$id=I('get.id'); // 取ID
		$one=$Model->where(" aid=$id ")->find();
		$this->assign('one', $one);
		
		$lang=empty($id)?'增加':'编辑';
		$this->assign('lang', $lang);
		
		$data=I('post.'); 
		
		if(!empty($data)){
			
			//验证
			if (!$Model->create()){     // 如果创建失败 表示验证没有通过 输出错误提示信息   
				$this->success($Model->getError(), U('activity/save'));
				die();
			}
			
			//读取配置文件数据
			$Conf=D("Config"); // 实例化config对象
			$conf_one=$Conf->where(' id=1 ')->find();
			
			//上传文件
			if(!empty($_FILES['thumb']['name'])){	
				$finfo=$this->uploadOne('thumb');	
			}
			
			
			
			$filesinfo='Public/'.$finfo['savepath'].$finfo['savename'];
			//打水印
			$water='';
			if(!empty($finfo)){
				if($conf_one['watermark'] !=0){
					$water=$this->water($filesinfo,$water_file = $conf_one['waterimage'],$conf_one['watermark']);
					}
				
				}
			
			
			//生成缩略图
			if(!empty($finfo)){
				if($conf_one['thumb'] !=0 && $water!=''){
					$this->thumb($water,$type=$conf_one['thumb'],$width=100,$height=100);
					}
				if($water=='' && $conf_one['thumb'] !=0){
					$this->thumb($filesinfo,$type=$conf_one['thumb'],$width=100,$height=100);
					}
			 	
			}
			
			if(empty($id)){
				$data['thumb']=$filesinfo;
				//增加活动
				//print_r($data);die;
				$insert=$Model->data($data)->add();
				if($insert>0){
						$this->success('增加成功', U('activity/list'));
					}else{
					 $this->error('增加失败');
					}
				}else{
					
					if(!empty($_FILES['thumb']['name'])){
						$data['thumb']=$filesinfo;	
					}else{
						$data['thumb']=$one['thumb'];	
						}
		
					//编辑活动
				 	$edit=$Model->where(" aid=$id ")->data($data)->save();
					if($edit){
						if(!empty($_FILES['thumb']['name'])){
							unlink($one['thumb']);
							$thumb_=dirname($one['thumb']).'/thumb_'.basename($one['thumb']);
							$water_=dirname($one['thumb']).'/water_'.basename($one['thumb']);
							unlink($thumb_);
							unlink($water_);
						}
						$this->success('编辑成功', U('activity/list'));
						die();
						}else{
					 		$this->error('编辑失败');
							die();
						}
					}
			}
			$this->display();
		}

		
	//活动删除
	public function delAction(){
		$Model=D("Activity"); // 实例化对象
		$id=I('get.id'); // 取ID
		$one=$Model->where(' aid='.$id)->find();
		if(!empty($one)){
			$del=$Model->where(" aid=$id ")->delete(); // 删除id为什么的用户数据
			if($del){
				unlink($one['thumb']);
				$thumb_=dirname($one['thumb']).'/thumb_'.basename($one['thumb']);
				$water_=dirname($one['thumb']).'/water_'.basename($one['thumb']);
				unlink($thumb_);
				unlink($water_);
				$this->success('删除成功', U('activity/list'));
				die();
				}else{
				$this->error('删除失败');
				die();
				}
			
		}	
	}
	
	
	//活动商品删除
	public function delhdspAction(){
		$Model=D("GoodsNActivity"); // 实例化对象
		$id=I('get.id'); // 取ID
		$one=$Model->where(' id='.$id)->find();
		if(!empty($one)){
			$del=$Model->where(" id=$id ")->delete(); // 删除id为什么的用户数据
			if($del){
					$this->success('删除成功', U('activity/activitygoods',array('id'=>$id,'status'=>1)));
					die();
				}else{
					$this->success('删除失败', U('activity/activitygoods',array('id'=>$id,'status'=>1)));
					die();
				}
			
		}	
	}
	
	
	
	//批量操作
	public function batchdelAction(){
		$Model=D("Activity"); // 实例化admin对象
		/*批量删除*/
		$batch=I('post.del');
		if(!empty($batch)){
			$id=I('post.id');
			foreach($id as $key=>$val){
				$one=$Model->where(' aid='.$val)->find();
				unlink($one['thumb']);
				$thumb_=dirname($one['thumb']).'/thumb_'.basename($one['thumb']);
				$water_=dirname($one['thumb']).'/water_'.basename($one['thumb']);
				unlink($thumb_);	
				unlink($water_);	
				$Model->where(" aid=$val ")->delete(); // 删除id为什么的用户数据		
				}
		}
		
		/*批量审核*/
		$batch=I('post.status1');
		if(!empty($batch)){	
			$id=I('post.id');
			foreach($id as $key=>$val){		
				$Model->where(" aid=$val ")->save($data=array('status'=>1)); // 根据条件更新记录
				}
		}
		
		/*批量未审核*/
		$batch=I('post.status0');
		if(!empty($batch)){	
			$id=I('post.id');
			foreach($id as $key=>$val){
				$Model->where(" aid=$val ")->save($data=array('status'=>0)); // 根据条件更新记录
				}
		}
		
		$this->success('操作成功', U('activity/list'));
		
	}		
	
	
	
		//活动商品列表
	public function activitygoodsAction(){
		$Model=D("GoodsNActivity"); // 实例化admin对象
		
		$aid=I('get.id',0,int);
		
		$this->assign('aid',$aid);// 赋值数据集
		
		//搜索
		$keyword=I('get.keyword');
		$status=I('get.status');
		if(!empty($keyword)){
			$where['title']  = array('like', '%'.$keyword.'%');
			$map['_complex'] = $where;
			if($status != -1){
				$map['status']  = $status;
			}
		}
		
		if(!empty($aid)){
			
			$map['aid']  = $aid;
		}
		
		
		//读取配置文件数据
		$Conf=D("Config"); // 实例化config对象
		$conf_one=$Conf->where(' id=1 ')->find();

		$count      = $Model->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$conf_one['Psize']);// 实例化分页类 传入总记录数和每页显示的记记录数
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');
		
		$Page->setConfig('header','<li><span class="rows">共 %TOTAL_ROW% 条记录</span></li>');
	
		
		$Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %PAGES%');
		
		$list = $Model->where($map)->relation('_goods')->order(' aid asc ')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		
		foreach($list as $k=>$v){
			
			$list[$k]['type']=$v['type']?'打折':'降价';
			
			$list[$k]['value']=$v['type']?$v['value'].'折':'降'.$v['value'].'元';
			
			$list[$k]['preferential']=$v['type']?($v['price']*($v['value']/10)):($v['price']-$v['value']);
		}
		
		//print_r($list);die;
		$this->assign('list',$list);// 赋值数据集
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出

		$this->display();
	}
	
	
	
	public function goodslistAction(){
		
		$Model=D("Goods"); // 实例化Goods对象	
		
		$aid=I('get.aid',0,int);
		
		$this->assign('aid',$aid);// 赋值数据集	
		
		//分类读取
		$clist=$this->categotyselect();
		$this->assign('clist',$clist);// 赋值数据集

		//品牌读取
		$blist=$this->brandselect();
		$this->assign('blist',$blist);// 赋值数据集
				
		//搜索
		$keyword=I('get.keyword');
		$status=I('get.status',-1,int);
		$catid=I('get.catid',0,int);
		$bid=I('get.bid',0,int);
		$tjian=I('get.tjian',-1,int);
		$frame=I('get.frame',-1,int);
		
		$now=$status;
		$this->assign('now', $now);
		
		if(!empty($keyword)){
			$where['title']  = array('like', '%'.$keyword.'%');
			$map['_complex'] = $where;
		}
		
		if($now == 1){
			$map['status']  = 1;
		}else{	
			$map['status']  = 2;
		}
		
		//分类
		if($catid != 0){
			$list=$this->getchinkid($catid);
			$map['catid']  = array('in',$list);
		}
		
		//品牌
		if($bid != 0){
			$map['bid']  = $bid;
		}
		
		
		if($tjian != -1){
			if($tjian==0){
				$map['new']  = 1;
			}else if($tjian==1){
				$map['hot']  = 1;	
			}else{
				$map['recommend']  = 1;		
			}	
		}
		
		
		if($frame != -1){
			if($frame==0){
				$map['frame']  = 0;	
			}else{
				$map['frame']  = 1;	
			}	
		}
		
		
		$liaid=$this->getagid();

		if(!empty($liaid)){
			$map['id']  = array('not in',$liaid);	
		}	
		


		//读取配置文件数据
		$Conf=D("Config"); // 实例化config对象
		$conf_one=$Conf->where(' id=1 ')->find();

		$count      = $Model->where($map)->count();// 查询满足要求的总记录数
		//echo $Model->getlastsql();
		$Page       = new \Think\Page($count,$conf_one['Psize']);// 实例化分页类 传入总记录数和每页显示的记录数		
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');		
		$Page->setConfig('header','<li><span class="rows">共 %TOTAL_ROW% 条记录</span></li>');		
		$Page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');			
		$list = $Model->where($map)->order(' id desc ')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		$this->display();

	}
	
	
	//查找正在进行活动或者还没有进行活动的商品ID
	public function getagoodsid(){
		
		$Model=D("Activity"); // 实例化对象
		$time=date("Y-m-d H:i:s ",time());
		$list=$Model->field('aid')->where(" endtiem > '$time' ")->select();
		//echo $Model->getlastsql();
		$listid=array();
		foreach($list as $k=>$v){
			
			$listid[]=$v['aid'];
		}

		return $listid;
		
	}

	
	//查找商品在这些ID里面
	public function getagid(){
		
		
		$Model=D("GoodsNActivity"); // 实例化对象
		
		$list=$this->getagoodsid();

		$map['aid']  = array('in',$list);
		$list=$Model->field('goodsid')->where($map)->select();
		
		$listid=array();
		foreach($list as $k=>$v){
			
			$listid[]=$v['goodsid'];
		}
		
		//print_r($listid);die;
		return $listid;
		
	}
	
	
	//参与活动商品
	public function partakeAction(){
		$Model=D("GoodsNActivity"); // 实例化对象
		$aid=I('get.aid',0,int);
		$goodsid=I('get.id',0,int);
		$this->assign('aid',$aid);// 赋值数据集
		$this->assign('goodsid',$goodsid);// 赋值数据集
		
		$data=I('post.');
		
		if(!empty($data)){
			
			$insert=$Model->data($data)->add();
			if($insert>0){
				$this->success('设置成功', "javascript:window.close()");
			}else{
				$this->success('设置失败', "javascript:window.close()");
			}
			
		}
		
		$this->display();
	}
	
	
	//编辑正在活动的商品
	public function dosaveAction(){
		$Model=D("GoodsNActivity"); // 实例化admin对象
		
		//编辑
		$id=I('get.id'); // 取ID
		$one=$Model->where(" aid=$id ")->relation('_goods')->find();
		$this->assign('one', $one);
		
		$lang=empty($id)?'增加':'编辑';
		$this->assign('lang', $lang);
		
		$data=I('post.'); 
		
		if(!empty($data)){
			
			  // print_r($data);
				//编辑
				$edit=$Model->where(" id=$id ")->data($data)->save();
				//echo $Model->getlastsql();die;
				if($edit){
					$this->success('编辑成功', U('activity/activitygoods',array('id'=>$id,'status'=>1)));
					die();
				}else{
					$this->error('编辑失败' ,U('activity/activitygoods',array('id'=>$id,'status'=>1)));
					die();
				}

			}
			$this->display();
		}
	
	
	
	/*
	查找分类下的所有子分类
	*/
	public function getchinkid($id,$array=array()){
		$Model=D("Category"); // 实例化对象			
		$array[]=$id;
		$Tlist=$Model->where(' parentid='.$id)->select();
		foreach($Tlist as $key=>$val){
			$array=$this->getchinkid($val['catid'],$array);
			}
			return $array;
	}
	
	//分类读取
	public function categotyselect(){
		$Role=D("Category"); // 实例化Category对象
		$category=$this->getcateg(0);
		return $category;
	}	
	
	//品牌读取
	public function brandselect(){
		$Brand=D("Brand"); // 实例化role对象
		$brand=$Brand->select();
		return $brand;
	}
	
	
		//读取分类树-------------------------------
	public function getcateg($pid,$retul=array(),$k=0){
		$Model=D("Category"); // 实例化对象
		
		
		$list=$Model->where(' parentid='.$pid)->select();
		
		
		foreach($list as $key=>$val){
			
			$chin=$this->getchin($val['catid']);
			if(empty($chin)){
					$chh=0;
				}else{
					$chh=1;
				}
			$val['chh']=$chh;
			
			$str='';
			for($i=0;$i<$k;$i++){
				$str.='|-';
				}
			$val['name']=$str.$val['name'];
					
			$retul[]=$val;	
			$retul=$this->getcateg($val['catid'],$retul,$k+1);
			
		}
		 return $retul;
	}
	
	
	public function getchin($pid){
		$Model=D("Category"); // 实例化对象		
		return $row=$Model->where(' parentid='.$pid)->find();
	}	
	
	
	
	
}